package battleroom;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.font.BitmapText;
import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;

/**
 * test
 * @author adam && zach
 * 
 * COLLISION GROUPS:
 * 1 - Player RigidBody Collids w/ 3
 * 2 - Player GhostControl collides 2/ 3
 * 3 - Stars 
 */
public class Main extends SimpleApplication {
    
    public boolean camFollow = true;
    public BulletAppState bulletAppState;
    
    private boolean shouldImpulse = false;
    private CameraNode camNode;
    private HUDGui hudGui;


    private Utility util;

    private MouseState mouseState;
    private Vector3f lastCam = new Vector3f();
      
    LevelState levelState = new LevelState();
    public Material defaultMatBlue;
    public Material defaultMatRed;
    public Material defaultMatGray;
    public Material defaultMatGreen; 
    PlayerState playerState;
    BaseGun playerGun; //Should be moved to player state
    
    

    public static void main(String[] args) 
    {
        Main app = new Main();
        app.start();    
    }
 

    public Utility getUtil()
    { return util; }

    @Override
    public void simpleInitApp() 
    {
        util = new Utility(this);
        
        defaultMatBlue = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        defaultMatBlue.setColor("Color", ColorRGBA.Blue);
        defaultMatRed = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        defaultMatRed.setColor("Color", ColorRGBA.Red);
        defaultMatGray = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        defaultMatGray.setColor("Color", ColorRGBA.Gray);
        defaultMatGreen = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        defaultMatGreen.setColor("Color", ColorRGBA.Green);
        
        setDisplayStatView(false);
        setDisplayFps(false);
        
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);         
        bulletAppState.getPhysicsSpace().setGravity(Vector3f.ZERO);
        stateManager.attach(levelState);
        stateManager.attach(GameState.get());
        GameState.get().setEnabled(false);
        
        playerState = new PlayerState(this);
        stateManager.attach(playerState);
        
        flyCam.setEnabled(true);
        flyCam.setMoveSpeed(100f);        
        
        hudGui = new HUDGui(settings, guiNode, assetManager);
        stateManager.attach(hudGui);
        
        
        playerGun = new BaseGun(this);
        playerGun.updateGunRotation();
        playerState.getRootNode().attachChild(playerGun.getSpatial());
        
        
        initCrossHairs();
        //Set up ambient lighting
        AmbientLight al = new AmbientLight();
        al.setColor(new ColorRGBA(1f, 1f, 1f, 1f));
        rootNode.addLight(al);
       /* ChaseCamera chaseCam = new ChaseCamera(cam, playerState.getRootNode(), inputManager);
        camNode = new CameraNode("camera", cam);
        camNode.setControlDir(ControlDirection.SpatialToCamera);
        playerState.getCamTarget().attachChild(camNode);
        camNode.setLocalTranslation(new Vector3f(-playerState.getCamTarget().getLocalTranslation().x, PlayerState.PlayerHeight*1.5f, -PlayerState.PlayerHeight*10));
        camNode.lookAt(playerState.getRootNode().getLocalTranslation(), Vector3f.UNIT_Y);
        
        mouseState = new MouseState(camNode, playerState.getRootNode());
        mouseState.setUpMouseListener(inputManager);
        */
        
        //Test
        /*Plane testPlane = new Plane();
        testPlane.setOriginNormal(Vector3f.ZERO, Vector3f.UNIT_X);
        PlaneCollisionShape testPlaneCollisionShape = new PlaneCollisionShape(testPlane);
        RigidBodyControl testBody = new RigidBodyControl(testPlaneCollisionShape);
        testBody.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_03);
        rootNode.addControl(testBody);
        bulletAppState.getPhysicsSpace().add(testBody);*/

 
        Box gateMesh = new Box(10f, 15f, 1f);

        Material testMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        //testMat.setColor("Color", ColorRGBA.Blue);
        testMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        testMat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Front);
       
        /*Box gateMesh = new Box(10f, 15f, 1f);
>>>>>>> .r61
        Geometry gate = new Geometry("holoGate", gateMesh);
        gate.setMaterial(testMat);
        rootNode.attachChild(gate);
        */
         
        
        /*Material mat1 = new Material(getAssetManager(), 
                                        "MatDefs/Lighting.j3md"); //
        mat1.setColor("Specular", ColorRGBA.White);
        mat1.setTexture("DiffuseMap", getAssetManager().loadTexture("Textures/stars/star_01.png"));
        mat1.setTexture("NormalMap", getAssetManager().loadTexture("Textures/stars/star_01_norm.png"));
        mat1.setFloat("Shininess", 5f);
        mat1.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Front);
        
        Spatial gate = assetManager.loadModel("Models/door.j3o");
        gate.setLocalScale(15f, 15f, 15f);
        gate.setMaterial(mat1);
        levelState.getRootNode().attachChild(gate);
        gate.addControl(new HoloGate(assetManager));
         * */
        
        initCrossHairs();

    }
    
    public HUDGui getHud()
    {
        return hudGui;
    }
    
    public AppSettings getSettings(){
            return settings;
    }
        
    protected void initCrossHairs() {
      guiNode.detachAllChildren();
      guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
      
      BitmapText ch = new BitmapText(guiFont, false);
      ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
      ch.setText("o"); // crosshairs
      ch.setLocalTranslation( // center              
      settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,
      settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);
      guiNode.attachChild(ch);
    }
    
    @Override
    public void simpleUpdate(float tpf) {
         
        if(GameState.get().isEnabled() == false && levelState.isInitialized())
        {
            GameState.get().setTargetCount(levelState.numTargets);
            GameState.get().setAllowedTime((float)levelState.numTargets*15); //15 seconds per target
            GameState.get().setEnabled(true);
        }
        
        playerGun.updateGunRotation();
        
        //The following statement is used in order to confine the lower right corner
        if(!(lastCam==cam.getDirection()))
            playerGun.getSpatial().setLocalTranslation(this.getCamera().getDirection().
                    cross(this.getCamera().getUp().mult(5)).
                    add(this.getCamera().getDirection().mult(7.5f)).
                    subtract(this.getCamera().getUp().mult(2f)));

        
        if(camFollow)
        {
            cam.setLocation(playerState.getRootNode().getLocalTranslation());
            //cam.setRotation(player.getLocalRotation());
        }

        lastCam.set(cam.getDirection());

        
        getUtil().update(tpf);

    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
    
    public boolean doImpulse()
    {
        return shouldImpulse;
    }
    
    public void doneImpulse()
    {
        shouldImpulse = false;
    }
    
    public PlayerState getPlayer()
    { return playerState; }

}